import * as cc from "cc";
import { GlobalConfig } from "../../GlobalConfig";

enum Key {
    /** 网络层日志 */
    Net = 'Net',
    /** 数据结构层日志 */
    Model = "Model",
    /** 业务逻辑层日志 */
    Business = "Business",
    /** 视图层日志 */
    View = "View",
    /** 配置数据 */
    Config = "Config",
    /** 普通日志 */
    Info = 'Info'
}
export namespace Loger {
    var tags: Map<string, boolean> = new Map<string, boolean>();
    export function init() {
        if (GlobalConfig.openLog || cc.sys.platform == cc.sys.Platform.DESKTOP_BROWSER) {
            register(Key.Net);
            register(Key.Model);
            register(Key.Business);
            register(Key.View);
            register(Key.Config);
            register(Key.Info);
        }
    }

    /** 开始计时 */
    export function start() {
        console.time("Time");
    }

    /** 打印范围内时间消耗 */
    export function end() {
        console.timeEnd("Time");
    }

    /** 打印表格 */
    export function table(msg: any) {
        if (!tags.has(Key.Info)) {
            return;
        }
        console.table(msg);
    }

    /** 无颜色日志 */
    export function log(...msg: any) {
        normal(Key.Info, msg)
    }

    /** 网络层数据日志 */
    export function logNet(...msg: any) {
        orange(Key.Net, msg);
    }

    /** 客户端数据结构层日志 */
    export function logModel(...msg: any) {
        violet(Key.Model, msg);
    }

    /** 客户端数据结构层日志 */
    export function logBusiness(...msg: any) {
        blue(Key.Business, msg);
    }

    /** 客户端数据结构层日志 */
    export function logView(...msg: any) {
        green(Key.View, msg);
    }

    /** 客户端配置数据 */
    export function logConfig(...msg: any) {
        gray(Key.Config, msg);
    }

    export function error(msg: any) {
        cc.error('[ERROR]' + msg);
    }

    export function warn(msg: any) {
        cc.warn('[WARN]:' + msg);
    }


    // 白色
    function normal(tag: string, msg: any) {
        print(tag, "", 5, msg)
    }

    // 橙色
    function orange(tag: string, msg: any) {
        print(tag, "color:#ee7700;",5, msg)
    }

    // 紫色
    function violet(tag: string, msg: any) {
        print(tag, "color:Violet;",5, msg)
    }

    // 蓝色
    function blue(tag: string, msg: any) {
        print(tag, "color:#3a5fcd;",5, msg)
    }

    // 绿色
    function green(tag: string, msg: any) {
        print(tag, "color:green;",5, msg)
    }

    // 灰色
    function gray(tag: string, msg: any) {
        print(tag,"color:gray;", 5, msg)
    }

    function register(tag: Key) {
        if (!tags.has(tag)) {
            tags.set(tag, true);
        }
    }

    function print(tag: string,  color: string, stackIndex:number, ...msg:any) {
        // 标记没有打开，不打印该日志
        if (!tags.has(tag)) {
            return;
        }
        var backLog = console.log || cc.log;
        backLog.call(this, "%c%s%s%s:%s", color, getDateString(), '[' + tag + ']', stack(stackIndex), cc.js.formatStr.apply(this,msg));
    }

    function stack(index: number): string {
        var e = new Error();
        var lines = e.stack!.split("\n");
        var result: Array<any> = [];
        lines.forEach((line) => {
            line = line.substring(7);
            var lineBreak = line.split(" ");
            if (lineBreak.length < 2) {
                result.push(lineBreak[0]);
            }else {
                result.push({ [lineBreak[0]]: lineBreak[1] });
            }
        });

        var list: string[] = [];
        var splitList: Array<string> = [];
        if (index < result.length) {
            var value: string;
            for (var a in result[index]) {
                var splitList = a.split(".");

                if (splitList.length == 2) {
                    list = splitList.concat();
                }else {
                    value = result[index][a];
                    var start = value!.lastIndexOf("/");
                    var end = value!.lastIndexOf(".");
                    if (start > -1 && end > -1) {
                        var r = value!.substring(start + 1, end);
                        list.push(r);
                    }else {
                        list.push(value);
                    }
                }
            }
        }

        if (list.length == 1) {
            return "[" + list[0] + ".ts]";
        }else if (list.length == 2) {
            return "[" + list[0] + ".ts->" + list[1] + "]";
        }
        return "Game";
    }

    function getDateString(): string {
        let d = new Date();
        let str = d.getHours().toString();
        let timeStr = "";
        timeStr += (str.length == 1 ? "0" + str : str) + ":";
        str = d.getMinutes().toString();
        timeStr += (str.length == 1 ? "0" + str : str) + ":";
        str = d.getSeconds().toString();
        timeStr += (str.length == 1 ? "0" + str : str) + ":";
        str = d.getMilliseconds().toString();
        if (str.length == 1) str = "00" + str;
        if (str.length == 2) str = "0" + str;
        timeStr += str;

        timeStr = "[" + timeStr + "]";
        return timeStr;
    }
}

Loger.init();